package com.threegroup.admin.controller;

import com.threegroup.admin.annotation.AuthorityVerify.AuthorityVerify;
import com.threegroup.admin.common.utils.ExcelUtils;
import com.threegroup.admin.dto.PasswordDTO;
import com.threegroup.admin.dto.SysUserDTO;
import com.threegroup.admin.excel.SysUserExcel;
import com.threegroup.admin.query.SysUserQuery;
import com.threegroup.admin.security.user.SecurityUser;
import com.threegroup.admin.service.ISysUserService;
import com.threegroup.common.utils.ConvertUtils;
import com.threegroup.common.utils.Result;
import com.threegroup.common.validator.AssertUtils;
import com.threegroup.common.validator.ValidatorUtils;
import com.threegroup.common.validator.group.AddGroup;
import com.threegroup.common.validator.group.DefaultGroup;
import com.threegroup.common.validator.group.UpdateGroup;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys")
public class SysUserController {

    @Resource
    private ISysUserService sysUserService;

    /**
     * 查询当前登录用户信息
     *
     * @return
     */
    @GetMapping("/user/info")
    public Result<SysUserDTO> info(){

        SecurityUser securityUser = (SecurityUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        SysUserDTO data = ConvertUtils.sourceToTarget(securityUser, SysUserDTO.class);
        return new Result<SysUserDTO>().ok(data);
    }

    /**
     * 分页查询用户
     * @param sysUserQuery 查询条件
     * @return
     */
    @AuthorityVerify("sys:user:page")
    @GetMapping("/user/page")
    public Result<Map<String, Object>> userList( SysUserQuery sysUserQuery) {

        return sysUserService.getUserList(sysUserQuery);

    }

    /**
     * 新增用户
     * @return
     */
    @AuthorityVerify("sys:user:save")
    @PostMapping("/user")
    public Result<String> save(@RequestBody SysUserDTO sysUserDTO) {

        // 校验数据合法性
        ValidatorUtils.validateEntity(sysUserDTO, AddGroup.class, DefaultGroup.class);

        sysUserService.save(sysUserDTO);

        return new Result<String>();
    }

    /**
     * 根据用户id查询用户
     * @param uid 用户id
     * @return
     */
    @AuthorityVerify("sys:user:info")
    @GetMapping("/user/{uid}")
    public Result<SysUserDTO> infoById(@PathVariable Long uid) {

        SysUserDTO sysUserDTO = sysUserService.getUserInfoById(uid);

        return new Result<SysUserDTO>().ok(sysUserDTO);
    }

    /**
     * 修改用户信息
     * @param sysUserDTO 用户信息
     * @return
     */
    @AuthorityVerify("sys:user:update")
    @PutMapping("/user")
    public Result<String> update(@RequestBody SysUserDTO sysUserDTO) {

        // 校验数据合法性
        ValidatorUtils.validateEntity(sysUserDTO, UpdateGroup.class, DefaultGroup.class);

        sysUserService.update(sysUserDTO);

        return new Result<>();
    }

    /**
     * 删除用户
     * @param ids 主键id数组
     * @return
     */
    @AuthorityVerify("sys:user:delete")
    @DeleteMapping("/user")
    public Result<String> delete(@RequestBody Long[] ids) {

        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        sysUserService.delete(ids);

        return new Result<>();
    }

    /**
     * 导出用户数据
     * @return
     */
    @AuthorityVerify("sys:user:export")
    @GetMapping("/user/export")
    public void export(SysUserQuery sysUserQuery, HttpServletResponse response) throws Exception {

        List<SysUserDTO> list = sysUserService.list(sysUserQuery);

        ExcelUtils.exportExcelToTarget(response, null, list, SysUserExcel.class);
    }

    /**
     * 修改密码
     * @return
     */
    @PutMapping("/user/password")
    public Result<String> editPassword(@RequestBody PasswordDTO passwordDTO) {

       return sysUserService.doEditPassword(passwordDTO);
    }
}
